package hello;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

public class Handler extends Thread {

    StreamConnection sc;
    OutputStream os;
    DataInputStream dataIn;
    DataOutputStream dataOut;
    TextField guessWord;
    StringItem announce;
    boolean notEnd;
    String ipServer;
    private Client cl;
   
    public boolean connectionNotEstablished = true;

    public Handler(String ipServer, TextField guessWord, StringItem announce, Client cl) {
        this.guessWord = guessWord;
        this.announce = announce;
        this.ipServer = ipServer;
        this.notEnd = true;
        this.cl = cl;
       
    }

    public synchronized void createInOutStream() {
        System.out.println("Before connect ad");
        try {
            sc = (StreamConnection) Connector.open("socket://" + ipServer + ":5000", Connector.READ_WRITE, true);
            System.out.println("Connected!!! right");
        } catch (IOException ex) {
            System.out.println("impossible to connect to the server");
            announce.setLabel("impossible to connect to the server");
            alert();
            closeConnection();
        } catch (NullPointerException e) {
            System.out.println("null pointer");
            alert();
            closeConnection();
        }

        if (sc != null) {
            cl.startHangman();
            System.out.println("After connec thread");
            InputStream is = null;
            try {
                is = sc.openInputStream();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            dataIn = new DataInputStream(is);

            // Output Stream
            os = null;
            try {
                os = sc.openOutputStream();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            dataOut = new DataOutputStream(os);
        }

    }

    public void send() {
        try {
            dataOut.writeChars(guessWord.getString());
            System.out.println("String send:" + guessWord.getString());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void run() {

        this.createInOutStream();
        char inter = 0;
        String notif = null;
        while (notEnd) {
            String result = "";
            try {
                while ((inter = dataIn.readChar()) != '0') {
                    if (inter == '1') {
                        notEnd = false;
                    } else if (inter == '2') {
                        notEnd = false;
                    } else {
                        result = result + inter;
                    }
                }
            } catch (IOException ex) {
                notif = "impossible to reach the server, quit the game";
                System.out.println("impossible to reach the server");
                notEnd = false;
                closeConnection();
            }
            System.out.println("GuessWord:" + result);
            guessWord.setLabel(result);
        }

        if (inter == '1') {
            notif = "You won, restart game";
        }
        if (inter == '2') {
            notif = "You lost, restart game";
        }
        announce.setLabel(notif);
    }

    public void closeConnection() {
        try {
            System.out.println("closing application");
            if (sc != null) {
                sc.close();
                //dataIn.close();
                //dataOut.close();
            }
            this.interrupt();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void alert() {
        cl.display.setCurrent(cl.connectForm);
        Alert a = new Alert("TimeClient", "Cannot connect to server. Ping the server to make sure it is running...", null, AlertType.ERROR);
        a.setTimeout(Alert.FOREVER);
        cl.display.setCurrent(a);
    }
}
